<template>
  <el-dialog title="新增字典项" :close-on-click-modal="false" @close="close" :visible.sync="visible" top="10%">
    <el-form :model="form" :rules="rules" ref="form" size="mini">
      <el-row>
        <el-col :span="12">
          <el-form-item label="中文名称" :label-width="formLabelWidth" prop="cname">
            <el-input v-model="form.cname"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="英文名称" :label-width="formLabelWidth" prop="ename">
            <el-input v-model="form.ename"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="CODE" :label-width="formLabelWidth" prop="code">
            <el-input v-model="form.code"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序号" :label-width="formLabelWidth" prop="sortNo">
            <el-input v-model="form.sortNo"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" :label-width="formLabelWidth" prop="valid">
            <el-select v-model="form.valid" placeholder="请选择">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.ke"
                  :label="item.value"
                  :value="item.key">
                </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <c-button type="primary" @click="btnOk">确 定</c-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapState } from 'vuex'
import Options from '@/constants/Options'

export default {
  name: 'SDialogDictItemAdd',
  props: {
    data: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    const _ = this.$lodash
    const validateIdentityCode = (rule, value, callback) => {
      if (_.isEmpty(value)) {
        callback()
      } else {
        this.$service.system.getUserByIdentityCode({identityCode: value}).then(function (res) {
          if (_.isEmpty(res.data)) {
            callback()
          } else {
            callback(new Error('该编号已被' + res.data.name + '使用'))
          }
        }).catch(function () {
          callback(new Error('校验失败请重新选择'))
        })
      }
    }
    return {
      formLabelWidth: '120px',
      visible: true,
      form: {
        type: this.data.type,
        cname: '',
        ename: '',
        code: '',
        sortNo: '',
        valid: ''
      },
      typeOptions: Options.ActiveOptions,
      rules: {
        cname: this.$checkRules.dictData.cname,
        ename: this.$checkRules.dictData.ename,
        code: this.$checkRules.dictData.code,
        sortNo: this.$checkRules.dictData.sortNo,
        valid: this.$checkRules.dictData.valid,
        identityCode: [{validator: validateIdentityCode, trigger: 'blur,change'}]
      }
    }
  },
  computed: {
    ...mapState('options', ['options'])
  },
  methods: {
    // 固定写法，每次加载都需要重置form
    close () {
      this.$emit('close')
    },
    btnOk (btn) {
      this.$refs['form'].validate((valid) => {
        // 校验通过
        if (valid) {
          this.$service.system.addDictData(this.form).then(() => {
            this.visible = false
            this.$emit('ok')
          }).finally(() => {
            btn.isLoading(false)
          })
        } else {
          btn.isLoading(false)
        }
      })
    }
  }
}
</script>

<style>
</style>
